package com.fangs.test;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther eerdunsang
 * @date 2020/10/29
 * @time 8:26.
 */
public class ItemNumberAdapter extends RecyclerView.Adapter<ItemNumberAdapter.ItemHolder> {
    private List<String> items;

    public ItemNumberAdapter(List<String> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, parent, false);

        //不允许用这个获取view！！
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, null);
        ItemHolder itemHolder = new ItemHolder(view);
        int parentHeight = parent.getHeight();
        parent.getWidth();
        ViewGroup.LayoutParams params = itemHolder.itemView.getLayoutParams();
        params.height = (parentHeight/5);   //需要展示几个item 就写几个
        return itemHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
        holder.tv.setText("当前"+items.get(position)+"："+position);
        if (position%2!=0){//奇数
            holder.tv.setBackgroundResource(R.color.purple_500);
        }else {//偶数
            holder.tv.setBackgroundResource(R.color.teal_200);
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public class ItemHolder extends RecyclerView.ViewHolder {
        private TextView tv;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}



